package com.test.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.test.domain.User;
import com.test.domain.Bill;
import com.test.repository.UserRepository;
import com.test.repository.BillRepository;

/**
 * 
 * @author Jeff Yang
 * @create 2017年12月4日
 */

@Controller
public class ReimburseController {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private BillRepository billRepository;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private TaskService taskService;

	@Autowired
	private HistoryService historyService;

	@GetMapping("/init")
	public String deploy() {
		InputStream inputStreambpmn = this.getClass().getResourceAsStream("/processes/reimburse.bpmn");
		InputStream inputStreampng = this.getClass().getResourceAsStream("/processes/reimburse.png");
		Deployment deployment = repositoryService// 与流程定义和部署对象相关的Service
				.createDeployment()// 创建一个部署对象
				.name("申请报销")// 添加部署的名称
				.addInputStream("reimburse.bpmn", inputStreambpmn)// 使用资源文件的名称（要求：与资源文件的名称要一致），和输入流完成部署
				.addInputStream("reimburse.png", inputStreampng)// 使用资源文件的名称（要求：与资源文件的名称要一致），和输入流完成部署
				.deploy();// 完成部署
		System.out.println("部署ID：" + deployment.getId());
		System.out.println("部署名称：" + deployment.getName());
		return "/manage";
	}

	@GetMapping("/complete/{taskId}/{userId}")
	public ModelAndView startInstance(@PathVariable("taskId") String taskId, @PathVariable("userId") Integer userId,
			Model model) {

		// Bill bill = billRepository.findOne(id);
		// bill.setStatus(1);
		// billRepository.save(bill);

		model.addAttribute("title", "任务列表");
		// model.addAttribute("billList", billRepository.findAll());
		taskService.complete(taskId);
		return new ModelAndView("redirect:/list/{userId}", "billModel", model);
	}

	@GetMapping("/create")
	public ModelAndView createForm(Model model) {
		model.addAttribute("bill", new Bill(null, null, null, null, null));
		model.addAttribute("title", "申请报销");
		return new ModelAndView("/createReimburse", "userModel", model);
	}

	@PostMapping("/saveBill/{id}")
	public ModelAndView saveBill(@PathVariable("id") Integer id, Bill bill, Model model) {
		bill = billRepository.save(bill);
		String processDefinitionId = "reimburse:1:10004";// 对应act_re_procdef表中的id
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("bill", bill);
//		ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, map);
		ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId);
		model.addAttribute("processInstance", processInstance);
		return new ModelAndView("redirect:/list/{id}");
	}

	@GetMapping("/list/{userId}")
	public ModelAndView listReimburse(@PathVariable("userId") Integer userId, Model model) {
		User user = userRepository.findOne(userId);
		List<Task> list = taskService.createTaskQuery().taskAssignee(user.getUsername()).list();
		model.addAttribute("billList", list);
		model.addAttribute("title", "任务列表");
		return new ModelAndView("/listReimburse", "billModel", model);
	}

}
